package com.yunhe.tianhe.rentcars.controller;

import com.yunhe.tianhe.rentcars.dto.JSONResponse;
import com.yunhe.tianhe.rentcars.entity.permission.Account;
import com.yunhe.tianhe.rentcars.entity.permission.Menu;
import com.yunhe.tianhe.rentcars.entity.permission.Resource;
import com.yunhe.tianhe.rentcars.service.LoginService;
import com.yunhe.tianhe.rentcars.service.permission.AccountService;
import com.yunhe.tianhe.rentcars.service.permission.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_DATE;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;


/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/10/16 0016 上午 10:44
 * @Description: 登录/退出控制器
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ResourceService resourceService;

    @RequestMapping
    public String verify(){
        return  "login";
    }

    @RequestMapping("/verify")
    @ResponseBody
    public JSONResponse login(HttpServletRequest req, HttpServletResponse resp, String userName, String password, String icode){
        String message="登录成功";
        int code = 1;
        if(null!=userName&&null!=password&&null!=icode){
            Account account = loginService.findByUser(userName, password);
            HttpSession session = req.getSession();
            String kaptcha = (String)session.getAttribute(KAPTCHA_SESSION_KEY);
            Date date = (Date)session.getAttribute(KAPTCHA_SESSION_DATE);
            if(null==kaptcha||!icode.equalsIgnoreCase(kaptcha)|| ((System.currentTimeMillis() - date.getTime()) /1000/ 60 > 15)){
                code = 0;
                message="验证码错误或失效";
            } else if (null!=account){
                session.setAttribute("account",account);
                List<Menu> menu = resourceService.findResourceMenu(account.getRoleId());
                session.setAttribute("menu",menu);
//                Cookie cookie = new Cookie("userName",userName);
//                cookie.setMaxAge(3600);
//                cookie.setPath("/");
//                resp.addCookie(cookie);
            }else {
                code=0;
                message="用户名或密码错误";
            }
            req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
            req.getSession().removeAttribute(KAPTCHA_SESSION_DATE);
        }
        return new JSONResponse(code,message);
    }

@RequestMapping("/page")
    public String mess(){
        return "/system/message/message";
}
@RequestMapping("/message")
@ResponseBody
public JSONResponse message(HttpServletRequest req,Account account_up){
    Account user =(Account) req.getSession().getAttribute("account");
    Account account = accountService.findOne(user.getuId());
    req.getSession().setAttribute("account",account);
    Long code = accountService.updatePerson(account_up);
    return JSONResponse.success("修改成功",code);
}    
}
